package com.cradom.event.controller;

import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.cradom.event.model.Account;
import com.cradom.event.model.Children;
import com.cradom.event.model.Event;
import com.cradom.event.model.Permission;
import com.cradom.event.model.Role;
import com.cradom.event.service.AuthorityService;
import com.cradom.event.service.ChildrenService;
import com.cradom.event.service.EventService;
import com.cradom.event.service.AccountService;
import com.cradom.event.utils.JsonUtil;

/**
 * 后台管理员操作控制类
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
	@Resource
	private EventService eventService;

	@Resource
	private AccountService accountService;

	@Resource
	private ChildrenService childrenService;
	
	@Resource
	private AuthorityService authorityService;

	/**
	 * 获取所有police_id为空的事件
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/newEvent")
	public String getNewEvent(HttpServletRequest request) {
		List<Event> eventList = eventService.getNewEvent();
		request.setAttribute("eventList", eventList);
		return "forward:/admin/accept.jsp";
	}

	/**
	 * 获取所有用户表
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/accountManager")
	public String getAccount(HttpServletRequest request) {
		List<Account> accountList = accountService.getAccount();
		request.setAttribute("accountList", accountList);
		return "forward:/admin/manager.jsp";
	}

	/**
	 * 获取所有宠物
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/childrenManager")
	public String getChildren(HttpServletRequest request) {
		List<Children> childrenList = childrenService.getChildren();
		request.setAttribute("childrenList", childrenList);
		return "forward:/admin/children.jsp";
	}

	/**
	 * 获取事件id，传到设置负责工作人员的from页
	 * 
	 * @param request
	 * @param event_id
	 * @return
	 */
	@RequestMapping("/toAccptForm")
	public String toAccptForm(HttpServletRequest request, @RequestParam("event_id") String event_id) {
		request.setAttribute("event_id", event_id);
		return "forward:/admin/acceptForm.jsp";
	}

	/**
	 * 根据事件id，设置负责工作人员id
	 * 
	 * @param request
	 * @param event_id
	 * @param police_id
	 * @return
	 */
	@RequestMapping(value = "/setPoliceID", method = RequestMethod.POST)
	public String setPoliceID(HttpServletRequest request, String event_id, String police_id) {
		Date police_id_time = new Timestamp(System.currentTimeMillis());
		try {
			eventService.setPoliceID(police_id, event_id, police_id_time);
		} catch (Exception e) {
			e.printStackTrace();
			return "forward:/error.jsp";
		}
		return "redirect:/admin/newEvent.do";
	}

	/**
	 * 消息列表
	 * 
	 * @param request
	 * @param session
	 * @param response
	 * @param event_id
	 * @throws Exception
	 */
	@RequestMapping(value = "/eventDropdown", method = RequestMethod.POST)
	public void getEventNoStartByID(HttpServletRequest request, HttpSession session, HttpServletResponse response,
			@RequestParam(value = "event_id", required = false) String event_id) throws Exception {
		// event_id不为空，设置police_start_time，再ajax请求
		if (event_id != null) {
			Date police_start_time = new Timestamp(System.currentTimeMillis());
			try {
				eventService.setStartTime(event_id, police_start_time);
			} catch (Exception e) {
				response.sendRedirect("/admin/error.jsp");
			}
		} else {
			Account account = (Account) session.getAttribute("admin");
			List<Event> listEvent = eventService.getEventNoStartByPhone(account.getPhone());
			JsonUtil<Event> jsonEventList = new JsonUtil<Event>();
			request.setCharacterEncoding("utf-8"); // 这里不设置编码会有乱码
			response.setContentType("text/html;charset=utf-8");
			response.setHeader("Cache-Control", "no-cache");
			PrintWriter out = response.getWriter(); // 输出中文，这一句一定要放到后面，否则中文返回到页面是乱码
			out.print(jsonEventList.arrayListToJson(listEvent));
		}
	}

	/**
	 * 去管理form
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/toManagerForm")
	public String toManagerForm(String id, HttpServletRequest request) {
		Account account = accountService.getAccountByID(id);
		List<Role> roleList=authorityService.getAllRole();
		request.setAttribute("account", account);
		request.setAttribute("roleList", roleList);
		return "forward:/admin/managerForm.jsp";
	}

	/**
	 * 根据帐号信息更新帐号
	 * 
	 * @param account
	 * @return
	 */
	@RequestMapping("/updateAccount")
	public String updateAccount(Account account) {
		try {
			accountService.updateAccount(account);
		} catch (Exception e) {
			return "redirect:/admin/error.jsp";
		}
		return "redirect:/admin/accountManager.do";
	}

	/**
	 * 根据用户id删用户
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteManager")
	public String deleteManager(String id) {
		try {
			accountService.deleteAccount(id);
		} catch (Exception e) {
			return "redirect:/admin/error.jsp";
		}
		return "redirect:/admin/accountManager.do";
	}
	
	/**
	 * 获取当前id的宠物信息，传入修改宠物信息form页面
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/toChildrenForm")
	public String toChildrenForm(String id,HttpServletRequest request){
		try{
			Children children = childrenService.getChildrenById(id);
			request.setAttribute("children", children);
		}catch(Exception e){
			return "redirect:/admin/error.jsp";
		}
		return "forward:/admin/childrenForm.jsp";
	}
	
	/**
	 * 根据id更新宠物信息
	 * @param children
	 * @return
	 */
	@RequestMapping("/updateChildren")
	public String updateChildren(Children children){
		try{
			childrenService.updateChildren(children);
		}catch(Exception e){
			return "redirect:/admin/error.jsp";
		}
		return "redirect:/admin/childrenManager.do";
	}
	
	/**
	 * 根据id删除宠物信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteChildren")
	public String deleteChildren(String id){
		try{
			childrenService.deleteChildren(id);
		}catch(Exception e){
			return "redirect:/admin/error.jsp";
		}
		return "redirect:/admin/childrenManager.do";
	}
	
	/**
	 * 获取所有角色和权限
	 * @param request
	 * @return
	 */
	@RequestMapping("/authorityManager")
	public String getAllAuthority(HttpServletRequest request){
		List<Role> roleList=authorityService.getAllRole();
		List<Permission> permissionList=authorityService.getAllPermission();
		request.setAttribute("roleList", roleList);
		request.setAttribute("permissionList", permissionList);
		return "forward:/admin/authority.jsp";
	}
	
	/**
	 * 添加角色
	 * @param role
	 * @return
	 */
	@RequestMapping("/addRole")
	public String addRole(Role role){
		try{
			authorityService.addRole(role);
		}catch(Exception e){
			return "redirect:/admin/error.jsp";
		}
		return "redirect:/admin/authorityManager.do";
	}
	
	/**
	 * 添加权限
	 * @param permission
	 * @return
	 */
	@RequestMapping("/addPermission")
	public String addPermission(Permission permission){
		try{
			authorityService.addPermission(permission);
		}catch(Exception e){
			return "redirect:/admin/error.jsp";
		}
		return "redirect:/admin/authorityManager.do";
	}
	
	/**
	 * 删除角色
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteRole")
	public String deleteRole(Integer id){
		try{
			authorityService.deleteRole(id);
		}catch(Exception e){
			return "redirect:/admin/error.jsp";
		}
		return "redirect:/admin/authorityManager.do";
	}
	
	/**
	 * 删除权限
	 * @param id
	 * @return
	 */
	@RequestMapping("/deletePermission")
	public String deletePermission(Integer id){
		try{
			authorityService.deletePermission(id);
		}catch(Exception e){
			return "redirect:/admin/error.jsp";
		}
		return "redirect:/admin/authorityManager.do";
	}
}
